﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Element.Net.Buttons
{
    public class ElementButton : Button
    {
        public ElementButton()
        {
            InitializeComponent();
        }

        public void InitializeComponent()
        {
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.FlatAppearance.MouseOverBackColor = Color.Transparent;
            this.FlatAppearance.MouseDownBackColor = Color.Transparent;
            this.ForeColor = Color.White;
            this.Width = 100;
            this.Height = 40;
        }

        private Color backgroundColor = Color.Silver;

        /// <summary>
        /// 按钮背景颜色
        /// </summary>
        [Browsable(true)]
        public Color BackgroundColor
        {
            get { return backgroundColor; }
            set { backgroundColor = value; }
        }

        private Color boarderColor = Color.Silver;

        /// <summary>
        /// 按钮边框颜色
        /// </summary>
        [Browsable(true)]
        public Color BoarderColor
        {
            get { return boarderColor; }
            set { boarderColor = value; }
        }

        private readonly StringFormat stringFormat = new StringFormat()
        {
            LineAlignment = StringAlignment.Center,
            Alignment = StringAlignment.Center
        };

        private int angle = 1;

        /// <summary>
        /// 弧度值
        /// </summary>
        /// [Browsable(true)]
        public int Angle
        {
            set { this.angle = value; }
            get { return angle > 0 ? angle : 1; }
        }

        private bool drawBorder = false;

        public bool DrawBorder { get => drawBorder; set => drawBorder = value; }

        /// <summary>
        /// 四边圆角
        /// </summary>
        public GraphicsPath GetGraphicsPath(int x, int y, int width, int height, int radius)
        {
            GraphicsPath GraphicsPath = new GraphicsPath();
            GraphicsPath.AddArc(x, y, radius, radius, 180, 90);
            GraphicsPath.AddArc(width - radius, y, radius, radius, 270, 90);
            GraphicsPath.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            GraphicsPath.AddArc(x, height - radius, radius, radius, 90, 90);
            GraphicsPath.CloseAllFigures();
            return GraphicsPath;
        }

        /// <summary>
        /// 绘画按钮
        /// </summary>
        private void DrawRectangle(Rectangle rectangle, Graphics graphics, int radius, Color color)
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            GraphicsPath path = GetGraphicsPath(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1, radius);
            graphics.FillPath(new SolidBrush(color), path);
            if (drawBorder) graphics.DrawPath(new Pen(boarderColor), path);
            graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectangle, stringFormat);
        }

        /// <summary>
        /// 重绘按钮
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = ClientRectangle;
            // pevent.ClipRectangle;// 绘画控件出现异常
            DrawRectangle(rectangle, graphics, angle, backgroundColor);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            //base.OnPaintBackground(pevent);
        }

    }
}
